package com.uenpay.oem.ui.trans.paycode;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.google.zxing.WriterException;
import com.qyx.android.message.type.PacketType;
import com.socks.library.KLog;
import com.uenpay.im.IMApplication;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.utilities.DensityUtils;
import com.uenpay.im.utilities.Toasts;
import com.uenpay.im.zxing.CaptureActivity;
import com.uenpay.im.zxing.encoding.EncodingHandler;
import com.uenpay.oem.MyApplication;
import com.uenpay.oem.R;
import com.uenpay.oem.common.ACacheConstant;
import com.uenpay.oem.data.bean.TransPayType;
import com.uenpay.oem.data.bean.WrapPayType;
import com.uenpay.oem.ui.reserver.AddnewcardActivity;
import com.uenpay.oem.util.BankCardUtil;
import com.uenpay.oem.widget.dialog.PayTypeListDialog;
import com.uenpay.service.bean.BankCardInfo;
import com.uenpay.service.bean.TransBankCardList;
import com.uenpay.util.app.base.BaseActivity;
import com.uenpay.util.common.ACache;
import com.uenpay.util.common.App;
import com.uenpay.util.crypt.SafeUrlCrypt;
import com.uenpay.util.imageloader.ImageLoader;
import com.uenpay.util.imageloader.ImageLoaderUtil;
import com.uenpay.util.imageloader.transform.GlideRoundTransform;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: liulou
 * Date: 2017/7/3.
 * desc: 付款码页面
 */

public class PayCodeActivity extends BaseActivity implements PayCodeContract.View {
    @BindView(R.id.iv_1d_code)
    ImageView iv1dCode;
    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;
    @BindView(R.id.iv_qr_code_avatar)
    ImageView ivQrCodeAvatar;
    @BindView(R.id.iv_paytype_logo)
    ImageView ivPaytypeLogo;
    @BindView(R.id.tv_paytype_name)
    TextView tvPaytypeName;
    @BindView(R.id.ll_choose_pay_type)
    LinearLayout llChoosePayType;
    @BindView(R.id.ll_paycode_scan)
    LinearLayout llPaycodeScan;

    private PayCodeContract.Presenter mPresenter;

    private PayTypeListDialog mPayTypeListDialog = null;
    private List<WrapPayType> mPaytypeList = null;
    private int mSelectedPosition = -1;
    private TransBankCardList<BankCardInfo> mTransBankCardList;

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_paycode;
    }

    @Override
    public void initView(View view) {
        setTitle("向商家付款");
        try {
            String data = "4556 3333 6768 898989";
            iv1dCode.setImageBitmap(EncodingHandler.CreateOneDCode(data, DensityUtils.dip2px(300), DensityUtils.dip2px(50)));
        } catch (WriterException e) {
            e.printStackTrace();
        }
        setPresenter(null);
        String data = APIConfiguration.getUenQrCodeUrl() + "Communal/Communal/getQrCodeInfo?data=" + SafeUrlCrypt.encodeByAes("adraiufhaiji9buvxsnivsjihfsihfn", SafeUrlCrypt.KEY) + "$0";
        mPresenter.getQrCode(data, getResources().getDimensionPixelSize(R.dimen.paycode_qrcode_size));
        mPresenter.getTransBankCardList();
    }

    @Override
    public void showQrCode(final Bitmap bitmap) {
        String avatarUrl = APIConfiguration.getAvatarUrlNormal(IMApplication.getCustId(), PacketType.PERSONAL) + "&date=" + System.currentTimeMillis();
        KLog.d(TAG, "avatarUrl" + avatarUrl);
        Glide.with(this).load(avatarUrl).asBitmap().skipMemoryCache(true).transform(new GlideRoundTransform(this, 6)).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                ivQrCodeAvatar.setImageBitmap(resource);
                ivQrCode.setImageBitmap(bitmap);
            }
        });

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }

    @Override
    public void setPresenter(Object presenter) {
        mPresenter = new PayCodePresenter(this);
    }

    @Override
    public void showToast(String msg) {
        Toasts.shortToast(this, msg);
    }

    @OnClick(R.id.ll_choose_pay_type)
    public void choosePayType() {
        if (mPayTypeListDialog == null) {
            mPaytypeList = getPayType();
            mPayTypeListDialog = new PayTypeListDialog(this, mPaytypeList);
            mPayTypeListDialog.setOnAddListener(new PayTypeListDialog.onAddClickListenner() {
                @Override
                public void onaddclick() {
                    //添加银行卡
                    Intent intent = new Intent(PayCodeActivity.this, AddnewcardActivity.class);
                    startActivity(intent);
                    mPayTypeListDialog.dismiss();
                }
            });
            mPayTypeListDialog.setOnChooseBankListener(new PayTypeListDialog.OnChooseBankListener() {
                @Override
                public void choose(int position) {
                    mSelectedPosition = position;
                    setSelectedPayType(mPaytypeList.get(position));
                }
            });

            //点击关闭
            mPayTypeListDialog.setOnCloseListener(new PayTypeListDialog.onCloseClickListener() {
                @Override
                public void oncloseClick() {
                    mPayTypeListDialog.dismiss();
                }
            });
        }
        if (mSelectedPosition != -1 && mPayTypeListDialog.getChoosePayTypeAdapter() != null) {
            mPayTypeListDialog.getChoosePayTypeAdapter().setChoosePosition(mSelectedPosition);
        }
        mPayTypeListDialog.show();
    }

    @Override
    public void setTransBankCardList(TransBankCardList<BankCardInfo> list) {
        mTransBankCardList = list;
    }

    /**
     * 封装
     *
     * @return
     */
    private List<WrapPayType> getPayType() {
        List<WrapPayType> payTypes = new ArrayList<>();
        payTypes.add(new WrapPayType(TransPayType.BALANCE, null));
        if (mTransBankCardList != null && mTransBankCardList.getBankCardList().size() > 0) {
            for (BankCardInfo info : mTransBankCardList.getBankCardList()) {
                payTypes.add(new WrapPayType(TransPayType.BANKCARD, info));
            }
        }
        return payTypes;
    }

    private void setSelectedPayType(WrapPayType payType) {
        TransPayType type = payType.getType();
        if (type == TransPayType.BALANCE) {
            tvPaytypeName.setText("账户余额");
            ivPaytypeLogo.setImageResource(R.drawable.ic_balance_small);
        } else if (type == TransPayType.BANKCARD) {
            BankCardInfo info = (BankCardInfo) payType.getObject();
            tvPaytypeName.setText(info.getBankName() + "(" + BankCardUtil.getLast4NumOfBankCard(info.getCardNo()) + ")");
            ivPaytypeLogo.setImageResource(BankCardUtil.getSmallLogoByBankName(info.getBankName()));
        } else if (type == TransPayType.ALIPAY) {
            tvPaytypeName.setText(R.string.paytype_alipay);
            ivPaytypeLogo.setImageResource(R.drawable.ic_alipay_logo);
        } else if (type == TransPayType.WEIXIN) {
            tvPaytypeName.setText(R.string.paytype_weixin);
            ivPaytypeLogo.setImageResource(R.drawable.ic_alipay_logo);
        }
    }


    @OnClick(R.id.ll_paycode_scan)
    public void scan() {
        Intent intent = new Intent(PayCodeActivity.this, CaptureActivity.class);
        startActivity(intent);
    }
}
